<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">

<HTML LANG="EN">

<HEAD>

<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<TITLE>Learning C from Java: Differences</TITLE>

<LINK REL=START HREF="./">

<META NAME="description" CONTENT="A list of differences between C and Java, assuming a previous knowledge with Java">

<LINK REL="STYLESHEET" HREF="style.css">

<!-- Java is a soft cushion at the bottom of the sea;
     C is a stoney floor at the bottom of a cliff. -->

</HEAD>

<BODY>

<H1>Learning C from Java</H1>

<DIV CLASS=NAV><P><SPAN CLASS=ITEM><A HREF="./">Contents</A></SPAN><SPAN CLASS=SEP> | </SPAN><SPAN CLASS=ITEM><A HREF="books.html">Books</A></SPAN><SPAN CLASS=SEP> | </SPAN><SPAN CLASS=ITEM><A HREF="links.html">Links</A></SPAN><SPAN CLASS=SEP> | </SPAN><SPAN CLASS=ITEM><A HREF="diffs.html">Differences</A></SPAN></P></DIV>

<H2>Differences</H2>

<P>This is a list of differences between Java and C, and assumes that
the reader knows less about the latter. It's worth familiarising
yourself with all the points, even if you don't fully understand them,
then you may be aware of the cause of any problem you might
encounter.</P>

<P>Many features of C, particularly its standard library facilities, are
not dealt with in any great depth, since you can look those up in help
files or manual pages, or a good reference book.</P>

<HR>

<DIV CLASS=CONTAINER>

<H3><A NAME="speedvport">Speed of execution <SPAN
CLASS=ITALIC>vs</SPAN> portability</A></H3>

<P>Java source and binaries are entirely portable (subject to
availability of appropriate libraries), since the source format is
standardized, and the binaries run on a software emulation of a
standardized processor, which also slows execution. In C, binaries are
not usually portable from one platform to another, because they use
the platform's native hardware processor directly, and so run faster,
but C source can be portable with little modification if it adheres to
the ISO&nbsp;C standard (ISO/IEC&nbsp;9899:1990, or C90), again
subject to the available libraries, while avoiding or accounting for
aspects of the standard which are implementation-defined.</P>

<P>There is now a new standard (ISO/IEC&nbsp;9899:1999, or C99) adding
some new features.  These are pointed out where appropriate.</P>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="execvdev">Speed of execution <SPAN CLASS=ITALIC>vs</SPAN>
speed/ease of development</A></H3>

<P>Java may initially be seen as a slow language, since it is compiled
from source into a bytecode, a low-level machine code for a
non-existent processor, which then has to be interpreted by a software
emulation of that processor.  All of this takes time, but this is less
significant in modern JVM implementations, which are able to compile
some or all of the program's bytecode into native code at execution
time (Just In Time, JIT).  For long running programs (e.g. GUI-based
applications, servers), this initial cost of translation is amortized
by the speed improvement achieved subsequently.  For short-lived
programs, like shell commands, this advantage cannot be taken.</P>

<P>Java programs may be easier to develop since:</P>

<UL>

<LI>dynamic memory management is handled largely automatically,
and</LI>

<LI>diagnostic exceptions are thrown for illegal operations (such as
accessing through a null reference, or accessing beyond the bounds of
an array).</LI>

</UL>

<P>Notwithstanding effective JIT optimization, C programs will usually
run faster, however, since:</P>

<UL>

<LI>dynamic memory management (which is often not required) is fully
under the programmer's control, and</LI>

<LI>there are no checks for illegal operations (but a well-written
program won't attempt them anyway),</LI>

</UL>

<P>&hellip;although these require greater responsibility from the
programmer.</P>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="types">Primitive types</A></H3>

<P>In C, the primitive types are referred to using a combination of
the keywords <CODE CLASS=C>char</CODE>, <CODE CLASS=C>int</CODE>,
<CODE CLASS=C>float</CODE>, <CODE CLASS=C>double</CODE>, <CODE
CLASS=C>signed</CODE>, <CODE CLASS=C>unsigned</CODE>, <CODE
CLASS=C>long</CODE>, <CODE CLASS=C>short</CODE> and <CODE
CLASS=C>void</CODE>. The allowable combinations are listed below, but
their meanings depend on the compiler and platform in use, unlike
Java.</P>

<DL>

<DT><CODE CLASS=C>unsigned char</CODE></DT>

<DD><P>The narrowest unsigned integral type, typically (and always at
least) 8 bits wide.</P></DD>

<DT><CODE CLASS=C>signed char</CODE></DT>

<DD><P>The narrowest signed integral type, of the same width as <CODE
CLASS=C>unsigned char</CODE>.</P></DD>

<DT><CODE CLASS=C>char</CODE></DT>

<DD><P>An integral type equivalent to one or other of the signed/unsigned
variants, but its signedness is implementation-dependent. C treats it as a
distinct type, though.</P></DD>

<DT><CODE CLASS=C>unsigned</CODE> <CODE CLASS=C>short</CODE> <CODE
CLASS=C>int</CODE></DT>

<DT><CODE CLASS=C>unsigned</CODE> <CODE CLASS=C>short</CODE></DT>

<DD><P>An unsigned integral type at least as wide as <CODE CLASS=C>unsigned
char</CODE>, typically (and always at least) 16 bits. The <CODE
CLASS=C>int</CODE> is usually omitted.</P></DD>

<DT><CODE CLASS=C>signed</CODE> <CODE CLASS=C>short</CODE> <CODE
CLASS=C>int</CODE></DT>

<DT><CODE CLASS=C>signed</CODE> <CODE CLASS=C>short</CODE></DT>

<DT><CODE CLASS=C>short</CODE> <CODE CLASS=C>int</CODE></DT>

<DT><CODE CLASS=C>short</CODE></DT>

<DD><P>A signed integral type of the same width as <CODE CLASS=C>unsigned
short int</CODE>. This is often just called <CODE
CLASS=C>short</CODE>.</P></DD>

<DT><CODE CLASS=C>unsigned</CODE> <CODE CLASS=C>int</CODE></DT>

<DT><CODE CLASS=C>unsigned</CODE></DT>

<DD><P>An unsigned integral type at least as wide as <CODE
CLASS=C>unsigned short</CODE>, and wider than the <CODE
CLASS=C>char</CODE> types. 16- or 32-bit widths are common.</P></DD>

<DT><CODE CLASS=C>signed</CODE> <CODE CLASS=C>int</CODE></DT>

<DT><CODE CLASS=C>signed</CODE></DT>

<DT><CODE CLASS=C>int</CODE></DT>

<DD><P>A signed integral type of the same size as <CODE CLASS=C>unsigned
int</CODE>. This is often just called <CODE
CLASS=C>int</CODE>.</P></DD>

<DT><CODE CLASS=C>unsigned</CODE> <CODE CLASS=C>long</CODE> <CODE
CLASS=C>int</CODE></DT>

<DT><CODE CLASS=C>unsigned</CODE> <CODE CLASS=C>long</CODE></DT>

<DD><P>An unsigned integral type at least as wide as <CODE
CLASS=C>unsigned int</CODE>, typically (and always at least) 32
bits. The <CODE CLASS=C>int</CODE> is often omitted.</P></DD>

<DT><CODE CLASS=C>signed</CODE> <CODE CLASS=C>long</CODE> <CODE
CLASS=C>int</CODE></DT>

<DT><CODE CLASS=C>signed</CODE> <CODE CLASS=C>long</CODE></DT>

<DT><CODE CLASS=C>long</CODE> <CODE CLASS=C>int</CODE></DT>

<DT><CODE CLASS=C>long</CODE></DT>

<DD><P>A signed integral type of the same size as <CODE CLASS=C>unsigned
long int</CODE>. This is often just called <CODE
CLASS=C>long</CODE>.</P></DD>



<DT><CODE CLASS=C>unsigned</CODE> <CODE CLASS=C>long</CODE> <CODE
CLASS=C>long</CODE> <CODE CLASS=C>int</CODE></DT>

<DT><CODE CLASS=C>unsigned</CODE> <CODE CLASS=C>long</CODE> <CODE
CLASS=C>long</CODE></DT>

<DD><P><STRONG>In C99,</STRONG> an unsigned integral type at least as
wide as <CODE CLASS=C>unsigned long</CODE>, typically (and always at
least) 64 bits. The <CODE CLASS=C>int</CODE> is often
omitted.</P></DD>


<DT><CODE CLASS=C>signed</CODE> <CODE CLASS=C>long</CODE> <CODE
CLASS=C>long</CODE> <CODE CLASS=C>int</CODE></DT>

<DT><CODE CLASS=C>signed</CODE> <CODE CLASS=C>long</CODE> <CODE
CLASS=C>long</CODE></DT>

<DT><CODE CLASS=C>long</CODE> <CODE CLASS=C>long</CODE> <CODE
CLASS=C>int</CODE></DT>

<DT><CODE CLASS=C>long</CODE> <CODE CLASS=C>long</CODE></DT>

<DD><P><STRONG>In C99,</STRONG> a signed integral type of the same
size as <CODE CLASS=C>unsigned long long int</CODE>.  This is often
just called <CODE CLASS=C>long long</CODE>.</P></DD>





<DT><CODE CLASS=C>float</CODE></DT>

<DD><P>A single precision floating-point type.</P></DD>

<DT><CODE CLASS=C>double</CODE></DT>

<DD><P>A double precision floating-point type.</P></DD>

<DT><CODE CLASS=C>long</CODE> <CODE CLASS=C>double</CODE></DT>

<DD><P>An extended double precision floating-point type.</P></DD>

<DT><CODE CLASS=C>void</CODE></DT>

<DD><P>An empty type. It has no value, and cannot be accessed. As in
Java, C functions with no return value are defined to return <CODE
CLASS=C>void</CODE>. Unlike Java, a function with no parameters has
<CODE CLASS=C>void</CODE> in its parameter list.</P></DD>

</DL>

<P>Note that there is no boolean type. Instead, the test conditions of
<CODE CLASS=C>if</CODE>, <CODE CLASS=C>while</CODE> and <CODE
CLASS=C>for</CODE> statements, and the operands of the logical
operators (<CODE CLASS=C>!</CODE>, <CODE CLASS=C>&amp;&amp;</CODE> and
<CODE CLASS=C>||</CODE>), are integer expressions with a boolean
interpretation: zero means false, non-zero means true. The relational
operators (<CODE CLASS=C>==</CODE>, <CODE CLASS=C>!=</CODE>, <CODE
CLASS=C>&lt;=</CODE>, <CODE CLASS=C>&gt;=</CODE>, <CODE
CLASS=C>&lt;</CODE> and <CODE CLASS=C>&gt;</CODE>) and logical
operators return <CODE CLASS=C>0</CODE> for false and <CODE
CLASS=C>1</CODE> for true.</P>

<P><STRONG>In C99,</STRONG> there is a boolean type <CODE
CLASS=C>bool</CODE> (which is really just a very small integer type)
and symbolic values <CODE CLASS=C>true</CODE> and <CODE
CLASS=C>false</CODE> (i.e. just 1 and 0), but the other integer types
work just as well as before.</P>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="comments">Comments</A></H3>

<P>Java allows the use of these forms of comment:</P>

<PRE CLASS=JAVA>
/* <SPAN CLASS=COMMENT>a multiline
   comment</SPAN> */
// <SPAN CLASS=LINECOMMENT>a single line comment</SPAN>
</PRE>

<P>C only allows the former. It is not wise to use such comments to
temporarily disable sections of code, since they do not nest. Use the
preprocessor (<A HREF="#preproc">see later</A>) instead:</P>

<PRE CLASS=C>
  /* <SPAN CLASS=COMMENT>enabled code</SPAN> */
#if 0
  /* <SPAN CLASS=COMMENT>disabled code</SPAN> */
#endif
  /* <SPAN CLASS=COMMENT>enabled code</SPAN> */
</PRE>

<P><STRONG>In C99,</STRONG> the one-line comment is allowed.</P>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="noclasses">Structures instead of classes</A></H3>

<P>C does not allow you to declare class types (as you can in Java
using the <CODE CLASS=JAVA>class</CODE> construct), but you can declare
C structures using the <CODE CLASS=C>struct</CODE> construct. A C
structure is like a Java class that only contains public data members
&#8212; there must be no functions, and all parts are visible to any
code that knows the declaration. For example:</P>

<PRE CLASS=C>
struct point {
  int x, y;
};
</PRE>

<P>This declares a type called <CODE CLASS=C>struct point</CODE> (NB:
&lsquo;<CODE CLASS=C>struct</CODE>&rsquo; is part of the name; <CODE
CLASS=C>point</CODE> is known as the structure type's
<DFN>tag</DFN>).</P>

<P>Members of a C structure are accessed using the <CODE
CLASS=C>.</CODE> operator, as class members can be in Java:</P>

<PRE CLASS=C>
struct point location;

location.x = 10;
location.y = 13;
</PRE>

<P>A structure object may be initialised where it is defined:</P>

<PRE CLASS=C>
struct point location = { 10, 13 }; /* <SPAN CLASS=COMMENT>okay; initialisation (part of definition)</SPAN> */

location = { 4, 5 }; /* <SPAN CLASS=COMMENT>illegal; assignment (not part of definition)</SPAN> */
</PRE>

<P><STRONG>In C99,</STRONG> you can create anonymous structure objects
to perform compound assignement:</P>

<PRE CLASS=C>
location = (struct point) { 4, 5 }; /* <SPAN CLASS=COMMENT>legal in C99</SPAN> */
</PRE>

<P><STRONG>In C99,</STRONG> a structure initialisation can specify
which members are being set:</P>

<PRE CLASS=C>
struct point location = { .y = 13, .x = 10 }; /* <SPAN CLASS=COMMENT>legal in C99</SPAN> */
</PRE>

<P>Unlike Java, where class variables are references to objects, C
structure variables are the objects themselves. Assigning one to
another causes copying of the members:</P>

<PRE CLASS=C>
struct point a = { 1, 2 };
struct point b;

b = a;    /* <SPAN CLASS=COMMENT>copies <CODE CLASS=C>a.x</CODE> to <CODE CLASS=C>b.x</CODE>, and <CODE CLASS=C>a.y</CODE> to <CODE CLASS=C>b.y</CODE></SPAN> */
b.x = 10; /* <SPAN CLASS=COMMENT>does not affect <CODE CLASS=C>a.x</CODE></SPAN> */
</PRE>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="enums">Enumerations</A></H3>

<P>An enumeration defines several symbolic integer constants with
unique values in a convenient way. The following declares a new type
<CODE CLASS=C>enum light</CODE>, and defines the symbols <CODE
CLASS=C>RED</CODE> for <CODE CLASS=C>0</CODE>, <CODE
CLASS=C>REDAMBER</CODE> for <CODE CLASS=C>1</CODE>, <CODE
CLASS=C>GREEN</CODE> for <CODE CLASS=C>2</CODE>, and <CODE
CLASS=C>AMBER</CODE> for <CODE CLASS=C>3</CODE>:</P>

<PRE CLASS=C>
enum light { RED, REDAMBER, GREEN, AMBER };
</PRE>

<P>The first symbol is assigned the value <CODE CLASS=C>0</CODE>, and
each subsequent symbol is assigned the next integer. However, a symbol
can be assigned a particular value:</P>

<PRE CLASS=C>
enum light { RED = 3, REDAMBER, GREEN = 1, AMBER };
</PRE>

<P>This also implies that <CODE CLASS=C>REDAMBER</CODE> is <CODE
CLASS=C>4</CODE>, and that <CODE CLASS=C>AMBER</CODE> is <CODE
CLASS=C>2</CODE>.</P>

<P>If a new type is not required, the tag can be omitted:</P>

<PRE CLASS=C>
enum { RED, REDAMBER, GREEN, AMBER };
</PRE>

<P>The symbols can be used in any expression, and may be assigned to
any integral type, not just the <CODE CLASS=C>enum</CODE> type. For
this reason, the tag is rarely used.</P>

<P>Symbolic constants in Java usually have this form:</P>

<PRE CLASS=JAVA>
  public static final int RED = 0;
  public static final int REDAMBER = 1;
  public static final int GREEN = 2;
  public static final int AMBER = 3;
</PRE>

<P>However, Java&nbsp;1.5 has introduced a new <CODE
CLASS=JAVA>enum</CODE> family of classes, which achieves the above
with greater type-safety, and a few other nice facilities:</P>

<PRE CLASS=JAVA>
  public enum LightState { RED, REDAMBER, GREEN, AMBER }
</PRE>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="unions">Unions</A></H3>

<P>C allows an area of memory to be occupied by data of several types,
though only one at a time, using a union. Unions are syntactically
similar to structures:</P>

<PRE CLASS=C>
union number {
  char c;
  int i;
  float f;
  double d;
};
</PRE>

<P>This declares a type called <CODE CLASS=C>union number</CODE> (NB:
&lsquo;<CODE CLASS=C>union</CODE>&rsquo; is part of the name; <CODE
CLASS=C>number</CODE> is known as the union's <DFN>tag</DFN>).</P>

<P>Members of a C union are accessed using the <CODE
CLASS=C>.</CODE> operator, just as structure members are accessed:</P>

<PRE CLASS=C>
union number n;
int j;

n.i = 10;
j = n.i;
</PRE>

<P>Only the member to which a value was last assigned contains valid
information to be read. There is no way to determine that member
implicitly, so the programmer must take steps to identify it, for
example, by using a separate variable to indicate the type:</P>

<PRE CLASS=C>
union number n;
enum { CHAR, INT, FLOAT, DOUBLE } nt;

n.i = 10;
nt = INT;

switch (nt) {
case CHAR:
  /* <SPAN CLASS=COMMENT>access <CODE>n.c</CODE></SPAN> */
  break;
case INT:
  /* <SPAN CLASS=COMMENT>access <CODE>n.i</CODE></SPAN> */
  break;
case FLOAT:
  /* <SPAN CLASS=COMMENT>access <CODE>n.f</CODE></SPAN> */
  break;
case DOUBLE:
  /* <SPAN CLASS=COMMENT>access <CODE>n.d</CODE></SPAN> */
  break;
}
</PRE>

<P>Java does not have unions, although it is possible for a reference
to refer to any class derived from its own. A reference of type <CODE
CLASS=JAVA>java.lang.Object</CODE> can refer to any class of object,
since all classes are originally derived from <CODE
CLASS=JAVA>java.lang.Object</CODE>.</P>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="namespace">Single namespace for functions and global
variables</A></H3>

<P>Each class in Java defines a namespace which allows functions and
variables in separate, unrelated classes to share the same name. When
identifying a function or variable in Java, the namespace must be
expressed, or implied using an <CODE CLASS=JAVA>import</CODE>
directive; for example, the method <CODE
CLASS=JAVA>java.lang.Integer.toString()</CODE> is distinct from <CODE
CLASS=JAVA>java.lang.Long.toString()</CODE>. Java packages allow
distinct classes and interfaces to share the same name; for example,
the name <CODE CLASS=JAVA>Object</CODE> could refer to either
<CODE CLASS=JAVA>java.lang.Object</CODE> or <CODE
CLASS=JAVA>org.omg.CORBA.Object</CODE>.</P>

<P>In C, all functions are global,
and must share a single namespace (<SPAN CLASS=ITALIC>i.e.</SPAN> one
per program). Global variables can also be declared and defined, and
they also share that namespace. Care must be taken in choosing names
for functions in large projects, and often a strategy of using a
common prefix for groups of related functions is employed, <SPAN
CLASS=ITALIC>e.g.</SPAN> <CODE CLASS=C>WSA</CODE> prefixes most of the
WinSock functions.</P>

<P>Note that other namespaces exist in C: a single namespace is shared by
the tags of all structures, unions and enumerations; each structure
and union holds a unique namespace for its members; each block
statement holds a namespace for local variables.</P>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="overload">Lack of function name overloading</A></H3>

<P>In Java, two functions in the same namespace may share the same
name if their parameter types are sufficiently different. In C, this
is simply not the case, and all function names must be unique.</P>

<PRE CLASS=C>
void myfunc(int a)
{
  /* <SPAN CLASS=COMMENT>...</SPAN> */
}

void myfunc(float b) /* <SPAN CLASS=COMMENT>error: <CODE>myfunc</CODE> already defined</SPAN> */
{
  /* <SPAN CLASS=COMMENT>...</SPAN> */
}
</PRE>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="typedef">Type aliasing</A></H3>

<P>New names or aliases for existing types may be created using
<CODE CLASS=C>typedef</CODE>. For example:</P>

<PRE CLASS=C>
typedef int int32_t;
</PRE>

<P>This allows <CODE CLASS=C>int32_t</CODE> to be used anywhere in
place of <CODE CLASS=C>int</CODE>, and such aliases are often used to
hide implementation- or platform-specific details, or to allow the
choice of a widely-used type to be changed easily.</P>

<!-- A better example would involve arrays of pointers to arrays
etc. -->

<P><CODE CLASS=C>typedef</CODE> are also useful for expressing complex
compound types.  For example, a prototype for the standard-library
function <CODE CLASS=C>signal</CODE> has the following, rather cryptic
form (in ISO C):</P>

<PRE CLASS=C>
void (*signal(int signum, void (*handler)(int)))(int);
</PRE>

<P>Erm, what?  It becomes a little clearer when POSIX (an Operating
System standard which incorporates the C standard) declares it:</P>

<PRE CLASS=C>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
</PRE>

<P>Now we can see that the function's second argument has the same
type as its return value, and that that type is, in fact, a
pointer-to-function type.</P>

<P>Note that a <CODE CLASS=C>typedef</CODE> is <EM>syntactically</EM>
similar to a variable declaration, with the new type name appearing in
the place of the variable name.</P>

<P>There is no equivalent of type aliasing in Java.</P>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="decldefn">Declarations and definitions</A></H3>

<P>C programs are built from collections of functions (which have
behaviour) and objects (which have values; variables are objects), the
natures of which are indicated by their types. C compilers read
through source files sequentially, looking for names of types, objects
and functions being referred to by other types, objects and
functions.</P>

<P>A declaration of a type, object or function tells the compiler that
a name exists and how it may be used, and so may be referred to later
in the file. If the compiler encounters a name that does not have a
preceding declaration, it may generate an error or a warning because
it does not understand how the name is to be used.</P>

<P>In contrast, a Java compiler can look forward or back, or even into
other source files, to find definitions for referenced names.</P>

<P>A definition of an object or function tells the compiler which
module the object or function is in (see &ldquo;<A
HREF="diffs.html#build">Program modularity</A>&rdquo;). For an object,
the definition may also indicate its initial value. For a function,
the definition gives the function's behaviour.</P>

<H4><A NAME="protos">Functions and their prototypes</A></H4>

<P>In Java, the use of a function may appear earlier than its
definition. In C, all functions being used in a source file
<EM>should</EM> be declared somewhere earlier than their invocations
in that file, allowing the compiler to check if the arguments match
the function's formal parameters. A function declaration (or
<DFN>prototype</DFN>) looks like a function definition, but its body
(the code between and including the braces (&lsquo;<CODE
CLASS=C>{</CODE>&rsquo; and &lsquo;<CODE CLASS=C>}</CODE>&rsquo;) is
replaced by a semicolon (similar to a <CODE CLASS=JAVA>native</CODE>
method, or an interface method, in Java). If the compiler finds a
function invocation before any declaration, it will try to infer a
declaration from the invocation, and this may not match the true
definition. A proper declaration can be inferred from a function
definition, should that be encountered first.</P>

<PRE CLASS=C>
/* <SPAN CLASS=COMMENT>a declaration; parameter names may be omitted</SPAN> */
int power(int base, int exponent);

/* <SPAN CLASS=COMMENT>From here until the end of the file, we can make calls to <CODE>power()</CODE>,
   even though the definition hasn't been encountered.</SPAN> */

/* <SPAN CLASS=COMMENT>a definition; parameter names do not need to match declaration</SPAN> */
int power(int b, int e)
{
  int r = 1;
  while (e-- &gt; 0)
    r *= b;
  return r;
}
</PRE>

<H4><A NAME="globalobj">Global objects</A></H4>

<P>Global objects also have distinct declarative and definitive
forms. A definition may be accompanied by an initialiser, <SPAN
CLASS=ITALIC>e.g.</SPAN></P>

<PRE CLASS=C>
int globval = 34; /* <SPAN CLASS=COMMENT>initialised</SPAN> */
int another;      /* <SPAN CLASS=COMMENT>uninitialised</SPAN> */
</PRE>

<P>while a declaration should not have an initialiser, and should be
preceded by <CODE CLASS=C>extern</CODE>.</P>

<PRE CLASS=C>
extern int globval;
extern int another;
</PRE>

<P>(<CODE CLASS=C>extern</CODE> can also appear before a function
declaration, but it is optional.)</P>

<H4><A NAME="localobj">Local objects</A></H4>

<P>For local objects in C, the definition and declaration are not
distinguished. Unlike Java, all local variables must be defined at the
beginning of their enclosing block, before any statements are
reached.  <STRONG>This restriction does not apply in C99.</STRONG></P>

<PRE CLASS=C>
{
  int x; /* <SPAN CLASS=COMMENT>a definition</SPAN> */

  x = 10; /* <SPAN CLASS=COMMENT>a statement</SPAN> */

  int y; /* <SPAN CLASS=COMMENT>illegal; follows a statement</SPAN> */
}
</PRE>

<P>Furthermore, an iteration variable in a <CODE CLASS=C>for</CODE>
loop cannot be declared within the initialisation of the statement:</P>

<PRE CLASS=C>
{
  for (int x = 0; x &lt; 10; x++) { /* <SPAN CLASS=COMMENT>illegal</SPAN> */
    /* <SPAN CLASS=COMMENT>...</SPAN> */
  }
}
</PRE>

<P><STRONG>This restriction does not apply in C99.</STRONG></P>

<H4><A NAME="scope">Scope</A></H4>

<P>All declarations have scope, which is the part of the program in
which the declared name is valid. &#8216;File scope&#8217; means
<EM>from the declaration to the end of the file</EM>, and applies to
types, functions and global objects.</P>

<P>&#8216;Block scope&#8217; means <EM>from the declaration to the end
of the block statement in which it is declared</EM>. This always
applies to local objects (and formal parameters), but can also apply
to types, functions and global objects. All of the following
declarations have block scope, and can be used by the trailing
statements, but not beyond:</P>

<PRE CLASS=C>
{
  /* <SPAN CLASS=COMMENT>a local type</SPAN> */
  typedef int MyInteger;

  /* <SPAN CLASS=COMMENT>a local variable</SPAN> */
  MyInteger x;

  /* <SPAN CLASS=COMMENT>global variable</SPAN> */
  extern int y;

  /* <SPAN CLASS=COMMENT>function (<CODE>extern</CODE> is implicit)</SPAN> */
  int power(int base, int exponent);

  /* <SPAN CLASS=COMMENT>statements...</SPAN> */
}
</PRE>

<P>Unlike Java, a local variable in an inner block may hide one in an
outer block by having the same name:</P>

<PRE CLASS=C>
{
  int x;

  {
    int x; /* <SPAN CLASS=COMMENT>hides the other</SPAN> */
  }

  /* <SPAN CLASS=COMMENT>first one visible again</SPAN> */
}
</PRE>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="emptyparms">Empty parameter lists</A></H3>

<P>In Java, a function that takes no parameters is expressed using
<CODE CLASS=JAVA>()</CODE>. In C, such a function should be expressed
with <CODE CLASS=C>(void)</CODE> in its declaration and
definition. However, it is still invoked with <CODE
CLASS=C>()</CODE>:</P>

<PRE CLASS=C>
/* <SPAN CLASS=COMMENT>prototype/declaration</SPAN> */
int myfunc(void);

/* <SPAN CLASS=COMMENT>definition</SPAN> */
int myfunc(void)
{
  /* <SPAN CLASS=COMMENT>...</SPAN> */
}

/* <SPAN CLASS=COMMENT>invocation</SPAN> */
myfunc();
</PRE>

<P>The form <CODE CLASS=C>()</CODE> is permitted in declarations, but
it means &quot;unspecified arguments&quot; rather than &quot;no
arguments&quot;. This tells the compiler to abandon type-checking of
arguments where that function is invoked, and is not recommended.</P>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="build">Program modularity</A></H3>

<P>Java programs, particularly large ones, are usually built in a
modular fashion that supports code re-use. The source code is spread
over several source files (<SAMP>.java</SAMP>), and is used to
generate Java byte-code in class files (<SAMP>.class</SAMP>) which are
identified by the class they support, <STRONG>so in Java, there is a
direct relationship between the name of a class and the file
containing the code for that class</STRONG>. These are combined at
run-time to produce the executing program. Java's standard library of
utilities for file access, GUIs, internationalisation, <SPAN
CLASS=ITALIC>etc.</SPAN>, is a practical example of modular
coding.</P>

<P>A large C program may also be split into several source files
(usually with a <SAMP>.c</SAMP> extension), and compilation of each of
these produces an <DFN>object</DFN> file of (usually) the same name
with a different extension (<SAMP>.o</SAMP> or
<SAMP>.obj</SAMP>). These are the modules of C that can be combined to
form an executable program. An object file contains named
representations of the functions and global data defined in its source
file, and allows them to refer to other functions and data by name,
even if in a separate module. <STRONG>In C, there doesn't have to be
any relationship between the names of functions and variables and the
names of the modules that contain them.</STRONG></P>

<P>A final executable program is produced by supplying all the
relevant modules (as object files) to a <DFN>linker</DFN> (which is
often built into the compiler). This attempts to resolve all the
referred names into the memory addresses required by the generated
machine code, and <STRONG>linking will fail if some names cannot be
resolved, or if there are two representations of the same
name</STRONG>.</P>

<P>For example, the object file generated from the code below would
contain references to the names <SAMP>pow</SAMP> (because it is
invoked as a function) and <SAMP>errno</SAMP> (because it is accessed
as a global variable), and would also provide a representation of the
name <SAMP>func</SAMP> (because a definition of the function is
provided).</P>

<PRE CLASS=C>
extern int errno;

void func(void)
{
  double pow(double, double);
  double x = 3.0, y = 12.7, r;
  int e;

  r = pow(x, y);
  e = errno;

  /* <SPAN CLASS=COMMENT>...</SPAN> */
}
</PRE>

<P>Like Java, C comes with a standard library of general-purpose
support routines, an implementation of which is supplied with your
compiler. Its source code is not usually required, since it has
already been compiled into object files for your system, and these
will be used automatically when linking.</P>

<P>Other pre-compiled libraries may also exist (<SPAN
CLASS=ITALIC>e.g.</SPAN> to support sockets), but it will normally be
necessary to link with them explicitly to use them.</P>

<P>Here is an illustration of a program built from several
components:</P>

<DIV ALIGN=CENTER>
<OBJECT BORDER=0  DATA="cproj.svg" TYPE="image/svg+xml"
   WIDTH="80%" HEIGHT="76%">
<IMG SRC="cproj.png" ALT="[the figure]" BORDER=2 WIDTH=575 HEIGHT=629
     LONGDESC="prodesc.html">
</OBJECT>
</DIV>

<P>The source code consists of 4 source files (<SAMP>foo.c</SAMP>,
<SAMP>bar.c</SAMP>, <SAMP>baz.c</SAMP>, <SAMP>quux.c</SAMP>) and 3
header files for preprocessing (<SAMP>&quot;yan.h&quot;</SAMP>,
<SAMP>&quot;tan.h&quot;</SAMP>, <SAMP>&quot;tither.h&quot;</SAMP>; see
&quot;<A HREF="#include">File inclusion</A>&quot;). The program also
uses some header files (<SAMP>&lt;wibble.h&gt;</SAMP>,
<SAMP>&lt;wobble.h&gt;</SAMP>) from an additional library. Compiling
each of the source files in turn generates the object files
<SAMP>foo.o</SAMP>, <SAMP>bar.o</SAMP>, <SAMP>baz.o</SAMP>,
<SAMP>quux.o</SAMP>, and these are linked with an archive of
pre-compiled objects (<SAMP>libwubble.a</SAMP>) from the library to
produce an executable program <SAMP>myprog</SAMP>.</P>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="preproc">Preprocessing</A></H3>

<P>Each C source file undergoes a lexical preprocessing stage which
serves several purposes, including conditional compilation and macro
expansion. The main purpose is to allow common declarations of types,
global data and functions to be conveniently and consistently made
available to modules which need to access them. In general, the
preprocessor is able to insert, remove or replace text from the source
code as it is supplied to the compiler (the original source code
doesn't change).</P>

<P>There is no equivalent of preprocessing in Java, but the following
purposes don't usually apply to it anyway.</P>

<H4><A NAME="include">File inclusion</A></H4>

<P>When a large C program is split over several modules, code in one
module may need to make references to named code in another, or may
use types that the other module uses. The usual way to achieve this is
to precede the reference with a declaration that shows what the name
means. Some example declarations:</P>

<PRE CLASS=C>
/* <SPAN CLASS=COMMENT>this declares the type <CODE>struct point</CODE></SPAN> */
struct point {
  int x, y;
};

/* <SPAN CLASS=COMMENT>this declares the global variable <CODE>errno</CODE></SPAN> */
extern int errno;

/* <SPAN CLASS=COMMENT>this declares the function <CODE>getchar</CODE></SPAN> */
int getchar(void);
</PRE>

<P>It would be tedious to repeat such declarations in each source file
that requires them (particularly if they need to be modified as the
program develops), but these could instead be placed in a separate
file (usually with a <SAMP>.h</SAMP> extension), and inserted
automatically by the preprocessor when it encounters an <CODE
CLASS=C>#include</CODE> directive embedded in the source code, for
example:</P>

<PRE CLASS=C>
#include &quot;mydecls.h&quot;
</PRE>

<P>These <DFN>header</DFN> files are also preprocessed, and so may
contain further <CODE CLASS=C>#include</CODE> (or other)
directives.</P>

<P>Header files containing declarations for the standard library are
also available to the preprocessor. These are normally accessed with a
variant of the <CODE CLASS=C>#include</CODE> directive:</P>

<PRE CLASS=C>
/* <SPAN CLASS=COMMENT>include declarations for input/output routines</SPAN> */
#include &lt;stdio.h&gt;
</PRE>

<P>You should normally use the <CODE CLASS=C>&quot;&quot;</CODE> form
for your own headers rather than <CODE CLASS=C>&lt;&gt;</CODE>.</P>

<P><STRONG>Do not</STRONG> put definitions of functions or variables
in header files &#8212; it may result in multiple definitions of the
same name, so <STRONG>linking will fail</STRONG>. Header files should
normally only contain types, function prototypes, variable
declarations, and macro definitions.  Note that <A
HREF="#inline">inline functions</A> are exceptional.</P>

<H4><A NAME="macros">Macros</A></H4>

<P>The preprocessor allows macros to be defined which serve a number
of purposes:</P>

<UL>

<LI><P>Some macros are used to hold constants or expressions:</P>

<PRE CLASS=C>
#define PI 3.14159

double pi_twice = PI * 2;
</PRE>

<P><CODE CLASS=C>PI</CODE> will be replaced by the numeric value wherever it
is used.</P></LI>

<LI><P>Some macros take arguments:</P>

<PRE CLASS=C>
#define MAX(A,B) ((A) > (B) ? (A) : (B))
</PRE>

<P>that provide a convenient way to emulate functions without the
overhead of a real function call. (See a good book on C for the
limitations of this.)</P></LI>

<LI><P>Some macros are merely defined to exist:</P>

<PRE CLASS=C>
#define JOB_DONE
</PRE>

<P>and are used in conditional compilation.</P></LI>

</UL>

<H4><A NAME="condcomp">Conditional compilation</A></H4>

<P>The preprocessor allows code to be compiled selectively, depending
on some condition. For example, if we assume that the macro <CODE
CLASS=C>__unix__</CODE> is defined only when compiling for a UNIX
system, and that the macro <CODE CLASS=C>__windows__</CODE> is defined
only when compiling for a Windows system, then we could provide a
single piece of code containing two possible implementations depending
on the intended target:</P>

<PRE CLASS=C>
int file_exists(const char *name)
{
#if defined(__unix__)
  /* <SPAN CLASS=COMMENT>use UNIX system calls to find out if the file exists</SPAN> */
#elif defined(__windows__)
  /* <SPAN CLASS=COMMENT>use Windows system calls to find out if the file exists</SPAN> */
#else
  /* <SPAN CLASS=COMMENT>don't know what to do - abort compilation</SPAN> */
#error "No implementation for your platform."
#endif
}
</PRE>

<P>The most common use of conditional compilation, though, is to
prevent the declarations in a header file from being made more than
once, should the file be inadvertently <CODE CLASS=C>#include</CODE>d
more than once:</P>

<PRE CLASS=C>
/* <SPAN CLASS=COMMENT>in the file <SAMP>mydecls.h</SAMP></SPAN> */
#if !defined(mydecls_header)
#define mydecls_header

typedef int myInteger;

#endif
</PRE>

<P>You should normally protect all your header files in this way.</P>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="pointers">Pointers instead of references</A></H3>

<P>All variables of non-primitive types in Java are references. C
has no concept of &#8216;reference&#8217;, but instead has pointers,
which Java does not.</P>

<P>A pointer is an address in memory of some ordinary data. A variable
may be of pointer type, <SPAN CLASS=ITALIC>i.e.</SPAN> it holds the
address of some data in memory.</P>

<PRE CLASS=C>
/* <SPAN CLASS=COMMENT>we'll assume we're inside some block statement, as in a function</SPAN> */

int i, j; /* <SPAN CLASS=COMMENT><CODE>i</CODE> and <CODE>j</CODE> are integer variables</SPAN> */
int *ip;  /* <SPAN CLASS=COMMENT><CODE>ip</CODE> is a variable which can point to an integer variable</SPAN> */

i = 10;
j = 20;   /* <SPAN CLASS=COMMENT>values assigned</SPAN> */

ip = &amp;i;  /* <SPAN CLASS=COMMENT><CODE>ip</CODE> points to <CODE>i</CODE></SPAN> */
*ip = 5;  /* <SPAN CLASS=COMMENT>indirectly assign <CODE>5</CODE> to <CODE>i</CODE></SPAN> */
ip = &amp;j;  /* <SPAN CLASS=COMMENT><CODE>ip</CODE> points to <CODE>j</CODE></SPAN> */
*ip += 7; /* <SPAN CLASS=COMMENT><CODE>j</CODE> now contains <CODE>27</CODE></SPAN> */
i += *ip; /* <SPAN CLASS=COMMENT><CODE>i</CODE> now contains <CODE>32</CODE></SPAN> */
</PRE>

<DIV ALIGN=CENTER>
<OBJECT BORDER=0 DATA="ipai.svg" TYPE="image/svg+xml"
   WIDTH="27%" HEIGHT="11%">
<IMG SRC="ipai.png" ALT="" BORDER=2 WIDTH=192 HEIGHT=90>
</OBJECT>
</DIV>

<P>The <CODE CLASS=C>&amp;</CODE> operator obtains the address of a
variable (the syntax ensures that there is no conflict with the
bit-wise &#8216;and&#8217; operator). The <CODE CLASS=C>*</CODE>
operator <DFN>dereferences</DFN> the pointer (again, the syntax
ensures that there is no conflict with the multiplication operator). A
dereferenced pointer can be used on the left-hand side of an
assignment, <SPAN CLASS=ITALIC>i.e.</SPAN> it is a <DFN>modifiable
lvalue</DFN> (&#8216;el-value&#8217;), as in the two examples
above.</P>

<H4><A NAME="ptrtype">Pointer types</A></H4>

<P>For every type, there is a pointer type. Since there is an <CODE
CLASS=C>int</CODE> type, there is also a pointer-to-<CODE
CLASS=C>int</CODE> type, written <CODE
CLASS=C>int&nbsp;*</CODE>. <CODE CLASS=C>float&nbsp;*</CODE> is the
pointer-to-<CODE CLASS=C>float</CODE> type. When assigning a pointer
value to a variable, or comparing two pointer values, the types must
match. Given these declarations:</P>

<PRE CLASS=C>
int i, j;
float f;
int *ip;
float *fp;
</PRE>

<P>&#8230;then <CODE CLASS=C>i</CODE> is of type <CODE
CLASS=C>int</CODE>, so the expression <CODE CLASS=C>&amp;i</CODE> must
be of type <CODE CLASS=C>int&nbsp;*</CODE>. <CODE CLASS=C>ip</CODE> is
of type <CODE CLASS=C>int&nbsp;*</CODE>, so you can assign <CODE
CLASS=C>&amp;i</CODE> to it. <CODE CLASS=C>&amp;j</CODE> is of type
<CODE CLASS=C>int&nbsp;*</CODE>, so it can be compared with <CODE
CLASS=C>&amp;i</CODE>, and so on.</P>

<P>But <CODE CLASS=C>&amp;f</CODE> is of type <CODE
CLASS=C>float&nbsp;*</CODE>, so it cannot be assigned to <CODE
CLASS=C>ip</CODE>, or compared with <CODE CLASS=C>ip</CODE>, <CODE
CLASS=C>&amp;i</CODE> or <CODE CLASS=C>&amp;j</CODE>.</P>

<H4><A NAME="nullundef">Null and undefined pointers</A></H4>

<P>A valid value for a pointer may be null (it equals <CODE
CLASS=C>0</CODE>), indicating that it points to no object. <STRONG>Do
not dereference a null pointer.</STRONG> Many of the standard header
files define a macro for a null pointer, <CODE CLASS=C>NULL</CODE>,
which some programmers may prefer.</P>

<PRE CLASS=C>
#include &lt;stdlib.h&gt;

int *ip;

ip = NULL;
</PRE>

<DIV ALIGN=CENTER>
<OBJECT BORDER=0  DATA="null.svg" TYPE="image/svg+xml"
   WIDTH="13%" HEIGHT="11%">
<IMG SRC="null.png" ALT="" BORDER=2 WIDTH=94 HEIGHT=90>
</OBJECT>
</DIV>

<P>It is permissible to use pointers as integer expressions treated as
boolean expressions to detect a null pointer (null means
&#8216;false&#8217; in this context). For example:</P>

<PRE CLASS=C>
int *ip;

if (ip) {
  /* <SPAN CLASS=COMMENT><CODE>ip</CODE> is not null</SPAN> */
}

if (!ip) {
  /* <SPAN CLASS=COMMENT><CODE>ip</CODE> is null</SPAN> */
}
</PRE>

<P>Direct comparisons are also possible (<SPAN
CLASS=ITALIC>e.g.</SPAN> <CODE CLASS=C>ip != NULL</CODE>).</P>

<P>If a pointer variable has been neither initialised nor assigned the
address of a real object, it could be pointing anywhere, or be
null. <STRONG>Do not dereference such an undefined
pointer.</STRONG></P>

<DIV ALIGN=CENTER>
<OBJECT BORDER=0  DATA="undefined.svg" TYPE="image/svg+xml"
   WIDTH="25%" HEIGHT="11%">
<IMG SRC="undefined.png" ALT="" BORDER=2 WIDTH=178 HEIGHT=94>
</OBJECT>
</DIV>

<H4><A NAME="dangle">Dangling pointers</A></H4>

<P>In Java, an object will remain in existence so long as there is a
reference to it. In C, an object may go out of existence even if there are
pointers to it &#8212; the programmer is entirely responsible for
ensuring that pointers contain valid addresses (either <CODE
CLASS=C>0</CODE>, or the address of an existing object) when used. This
badly written function returns a pointer to an integer variable:</P>

<PRE CLASS=C>
int *badfunc(void)
{
  int x = 18;

  return &amp;x; /* <SPAN CLASS=COMMENT>bad - <CODE>x</CODE> won't exist after the call has finished</SPAN> */
}
</PRE>

<P>The pointer returned by <CODE CLASS=C>badfunc()</CODE> is invalid.</P>

<H4><A NAME="passbyref">Passing arguments by reference</A></H4>

<P>In Java, all primitive types are passed to functions by value
&#8212; the function is unable to change values of variables in the
invoking context. All reference types are passed by reference &#8212;
the function can alter the public contents of the referenced object.</P>

<P>In C, <EM>almost all</EM> types are passed by value, and so no
variables supplied as arguments can be altered by a function. It can
only alter its local copy of the variables. However, by passing a
pointer to the variable, the function is able to dereference its copy
of the pointer, and indirectly assign to the variable. Consider these
two functions which are intended to swap the values of two variables:</P>

<PRE CLASS=C>
void badswap(int a, int b)
{
  int tmp = b;
  b = a;
  a = tmp;
  /* <SPAN CLASS=COMMENT><CODE>a</CODE> and <CODE>b</CODE> are swapped but they're only copies</SPAN> */
}

void goodswap(int *ap, int *bp)
{
  int tmp = *bp;
  *bp = *ap;
  *ap = tmp;
}

/* <SPAN CLASS=COMMENT>assume we're in a function body</SPAN> */
int x = 10, y = 4;

printf("1: x = %d y = %d\n", x, y); /* <SPAN CLASS=COMMENT>print state of variables</SPAN> */
badswap(x, y);
/* <SPAN CLASS=COMMENT><CODE>x</CODE> and <CODE>y</CODE> are copied, and the copies are swapped
   so <CODE>x</CODE> and <CODE>y</CODE> are unchanged</SPAN> */
printf("2: x = %d y = %d\n", x, y);

goodswap(&amp;x, &amp;y);
/* <SPAN CLASS=COMMENT>pointers tell <CODE>goodswap()</CODE> where we store <CODE>x</CODE> and <CODE>y</CODE></SPAN> */
printf("3: x = %d y = %d\n", x, y);
</PRE>

<P>This reports:</P>

<PRE>
1: x = 10 y = 4
2: x = 10 y = 4
3: x = 4 y = 10
</PRE>

<P>&#8230;indicating that <CODE CLASS=C>badswap</CODE> had no effect on the
variables given as arguments.</P>

<DIV ALIGN=CENTER>
<OBJECT BORDER=0  DATA="goodswap.svg" TYPE="image/svg+xml"
   WIDTH="39%" HEIGHT="19%">
<IMG SRC="goodswap.png" ALT="" BORDER=2 WIDTH=275 HEIGHT=152>
</OBJECT>
</DIV>

<!-- P>In Java, a dynamic object is created using <CODE
CLASS=JAVA>new</CODE>, and is destroyed automatically when there are
no more references to it. In C, dynamic memory management is the
responsibility of the programmer. Space for a dynamic object is
allocated with a standard library call which returns a pointer to the
memory. Another call is used to release the memory by passing that
pointer to it.</P -->

<H4><A NAME="strptr">Pointers to structures and unions</A></H4>

<P>A pointer to a variable of structure type may exist. Accessing a
member of the structure is straight-forward: dereference the pointer,
and apply the <CODE CLASS=C>.</CODE> operator. However, parentheses
are needed to ensure the correct meaning, but a short form also exists
(and is widely used) for convenience:</P>

<PRE CLASS=C>
struct point loc;
struct point *locp = &amp;loc;

(*locp).x = 10; /* <SPAN CLASS=COMMENT>correct</SPAN> */
*locp.x = 10;   /* <SPAN CLASS=COMMENT>incorrect; same as <CODE>*(locp.x)</CODE></SPAN> */
locp->x = 10;   /* <SPAN CLASS=COMMENT>correct, shorter form</SPAN> */
</PRE>

<P>Syntactically, pointers to unions are accessed identically.</P>

<H4><A NAME="funcptr">Pointers to functions</A></H4>

<P>Functions also have addresses, for which there are
pointer-to-function types expressing the parameters and return
type. The pointers can be passed to or returned from other functions
just as other data can.</P>

<PRE CLASS=C>
void goodswap(int *, int *);
void (*swapfunc)(int *, int *); /* <SPAN CLASS=COMMENT>a pointer called <CODE>swapfunc</CODE></SPAN> */
int x, y;

swapfunc = &amp;goodswap;           /* <SPAN CLASS=COMMENT>now it points to a function</SPAN>
                                   <SPAN CLASS=COMMENT>with matching parameters</SPAN> */
(*swapfunc)(&amp;x, &amp;y);            /* <SPAN CLASS=COMMENT>invokes <CODE>goodswap(&amp;x, &amp;y)</CODE></SPAN> */
</PRE>

<P>Since pointers to functions are just values like any other, they
can be passed to and returned from functions, so that
&#8216;behaviour&#8217; becomes just another form of data.</P>

<H4><A NAME="ptrptr">Pointers to pointers</A></H4>

<P>A pointer may point to variable which itself holds another pointer,
and this is expressed in the pointer's type:</P>

<PRE CLASS=C>
int    i;           /* <SPAN CLASS=COMMENT><CODE>i</CODE> holds an integer</SPAN> */
int   *ip   = &amp;i;   /* <SPAN CLASS=COMMENT><CODE>ip</CODE> points to <CODE>i</CODE></SPAN> */
int  **ipp  = &amp;ip;  /* <SPAN CLASS=COMMENT><CODE>ipp</CODE> points to <CODE>ip</CODE></SPAN> */
int ***ippp = &amp;ipp; /* <SPAN CLASS=COMMENT><CODE>ippp</CODE> points to <CODE>ipp</CODE></SPAN> */
/* <SPAN CLASS=COMMENT>et cetera</SPAN> */
</PRE>

<P>The fact that the pointed-to object also holds a pointer does not
fundamentally change the behaviour of the pointer that points to
it. It just allows a further level of indirection &#8212; in practice,
you rarely need more than a couple of levels.</P>

<H4><A NAME="voidptr">Generic pointers</A></H4>

<P>It is sometimes necessary to store or pass pointers without knowing
what type they point to. For this, you can use the generic pointer
type <CODE CLASS=C>void&nbsp;*</CODE>. You can convert between the
generic pointer type and other pointer types (but not
pointer-to-function types) whenever you need to:</P>

<PRE CLASS=C>
int x;
int *xp, *yp;
void *vp;

xp = &amp;x;

vp = xp;   /* types are compatible */

/* later... */

yp = vp;   /* types are compatible */
</PRE>

<P>A generic pointer cannot be dereferenced, nor can <A
HREF="#arrays">pointer arithmetic</A> be applied to it.</P>

<PRE CLASS=C>
x = *vp;  /* <SPAN CLASS=COMMENT>error: cannot dereference <CODE>void *</CODE></SPAN> */
vp++;     /* <SPAN CLASS=COMMENT>error: cannot do arithmetic on <CODE>void *</CODE></SPAN> */
</PRE>

<P>The generic pointer type simply allows you to tell the compiler
that you're taking responsibility for a pointer's interpretation, and
so no error messages or warnings are to be reported when assigning. It
is the programmer's responsibility to ensure that the pointer value is
interpreted as the correct type.</P>

<PRE CLASS=C>
int *ip;
float *fp;
void *vp;

fp = ip;  /* <SPAN CLASS=COMMENT>error: incompatible types</SPAN> */
vp = ip;  /* <SPAN CLASS=COMMENT>okay</SPAN> */
fp = vp;  /* <SPAN CLASS=COMMENT>no compiler error, but is misuse</SPAN> */
</PRE>

<P>Generic pointers are used with <A HREF="#dynmem">dynamic memory
management</A>, among other things.</P>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="arrays">Arrays and pointer arithmetic</A></H3>

<P>Arrays in Java are reference types with automatic allocation of
memory. In C, arrays are groups of variables of the same type in
adjacent memory. Allocation for dynamic arrays is handled by the
programmer. An array of integers may look like this:</P>

<PRE CLASS=C>
int array[10]; /* <SPAN CLASS=COMMENT>numbered <CODE>0</CODE> to <CODE>9</CODE></SPAN> */
int i = 6;

array[3] = 12;
array[i] = 13;
</PRE>

<DIV ALIGN=CENTER>
<OBJECT BORDER=0  DATA="array.svg" TYPE="image/svg+xml"
   WIDTH="43%" HEIGHT="12%">
<IMG SRC="array.png" ALT="" BORDER=2 WIDTH=307 HEIGHT=99>
</OBJECT>
</DIV>

<H4><A NAME="arrayinit">Initialising arrays</A></H4>

<P>Arrays may be initialised when defined:</P>

<PRE CLASS=C>
int myArray[4] = { 9, 8, 7, 6 };
</PRE>

<P>The size is optional in this case, since the compiler sees that
there are four elements in the initialiser. The initialiser must not
be bigger than the size if specified, but it can be smaller. Either
way, the size must be known at compile time &#8212; it must not be an
expression in terms of the values of other objects or function
calls.</P>

<P><STRONG>In C99,</STRONG> you can specify which elements of an array
are initialised:</P>

<PRE CLASS=C>
int myArray[4] = { [2] = 7, [0] = 9, [1] = 8, [3] = 6 };
</PRE>

<H4><A NAME="arrayptr">Array-pointer relationship</A></H4>

<P>The address of an array element can be taken, and simple arithmetic
can be applied to it. Adding one to the address makes it point to the
next element in the array. Subtracting one instead makes it point to
the previous element.</P>

<PRE CLASS=C>
int myArray[4] = { 9, 8, 7, 6 };
int *aep = &amp;myArray[2];
int x, i;

*(aep + 1) = 2;   /* <SPAN CLASS=COMMENT>set <CODE>myArray[3]</CODE> to <CODE>2</CODE></SPAN> */
*(aep - 1) += 11; /* <SPAN CLASS=COMMENT>set <CODE>myArray[1]</CODE> to <CODE>19</CODE></SPAN> */
x = *(aep - 2);   /* <SPAN CLASS=COMMENT>set <CODE>x</CODE> to <CODE>9</CODE></SPAN> */
</PRE>

<DIV ALIGN=CENTER>
<OBJECT BORDER=0  DATA="arrayptr.svg" TYPE="image/svg+xml"
   WIDTH="40%" HEIGHT="24%">
<IMG SRC="arrayptr.png" ALT="" BORDER=2 WIDTH=289 HEIGHT=205>
</OBJECT>
</DIV>

<P>By definition, <CODE CLASS=C>*(aep&nbsp;+&nbsp;i)</CODE> is
equivalent to <CODE CLASS=C>aep[i]</CODE>, and in many contexts, an
array name such as <CODE CLASS=C>myArray</CODE> evaluates to the
address of the first element, which is how expressions such as <CODE
CLASS=C>myArray[2]</CODE> work (it becomes <CODE
CLASS=C>*(myArray&nbsp;+&nbsp;2)</CODE>). The code above could be
written as:</P>

<PRE CLASS=C>
int myArray[4] = { 9, 8, 7, 6 };
int *aep = &amp;myArray[2];
int x, i;

aep[1] = 2;    /* <SPAN CLASS=COMMENT>set <CODE>myArray[3]</CODE> to <CODE>2</CODE></SPAN> */
aep[-1] += 11; /* <SPAN CLASS=COMMENT>set <CODE>myArray[1]</CODE> to <CODE>19</CODE></SPAN> */
x = aep[-2];   /* <SPAN CLASS=COMMENT>set <CODE>x</CODE> to <CODE>9</CODE></SPAN> */
</PRE>

<P>Note that an array name such as <CODE CLASS=C>myArray</CODE> can not
be made to point elsewhere:</P>

<PRE CLASS=C>
int myArray[4];
int i;
int *ip;

ip = myArray; /* <SPAN CLASS=COMMENT>okay: <CODE>myArray</CODE> is a legal expression; <CODE>ip</CODE> now points to <CODE>myArray[0]</CODE></SPAN> */
myArray = &amp;i; /* <SPAN CLASS=COMMENT>error: <CODE>myArray</CODE> is not a variable</SPAN> */
</PRE>

<H4><A NAME="arrayfunc">Passing arrays to functions</A></H4>

<P>Arrays are effectively passed to functions by reference. The array
name evaluates to a pointer to the first element, so the function's
parameter has a type of &#8216;pointer-to-element-type&#8217;. For
example, given the function:</P>

<PRE CLASS=C>
void fill_array_with_square_numbers(int *first, int length)
{
  int i;

  for (i = 0; i < length; i++)
    first[i] = i * i;
}
</PRE>

<P>we could write code such as:</P>

<PRE CLASS=C>
int squares[4], moresquares[10];
void fill_array_with_square_numbers(int *first, int length);

fill_array_with_square_numbers(squares, 4);
fill_array_with_square_numbers(moresquares + 2, 7);
</PRE>

<P>The second call only fills part of the array <CODE
CLASS=C>moresquares</CODE>.</P>

<DIV ALIGN=CENTER>
<OBJECT BORDER=0  DATA="moresquare.svg" TYPE="image/svg+xml"
   WIDTH="48%" HEIGHT="21%">
<IMG SRC="moresquare.png" ALT="" BORDER=2 WIDTH=338 HEIGHT=174>
</OBJECT>
</DIV>

<P>Note that the programmer must take steps to indicate the length of
the array, in this case by defining the function to take a length
argument (an alternative would be to identify a special value within
the array to mark its end). The second call only has elements 2 to 8
set (an array of length 7).</P>

<H4><A NAME="arraylen">Array length</A></H4>

<P>Because <CODE CLASS=C>squares</CODE> above is the <EM><STRONG>name</STRONG> of an array</EM>,
we can obtain its length using <CODE
CLASS=C>sizeof&nbsp;squares</CODE>, which returns the total size as a
number of <CODE CLASS=C>char</CODE>s. <CODE
CLASS=C>sizeof&nbsp;squares[0]</CODE> returns the size (in <CODE
CLASS=C>char</CODE>s) of one element, and since all the elements are
of the same size, the ratio of these two <CODE CLASS=C>sizeof</CODE>s
is the number of elements in the array:</P>

<PRE CLASS=C>
fill_array_with_square_numbers(squares, sizeof squares / sizeof squares[0]);
</PRE>

<P>(For arrays of <CODE CLASS=C>char</CODE>s, the divisor can be
omitted, since <CODE CLASS=C>sizeof(char)</CODE> is defined to be
<CODE CLASS=C>1</CODE>.)</P>

<P>However, this technique doesn't work if the argument to <CODE
CLASS=C>sizeof</CODE> is a pointer to the first element of an array:
consider that such a pointer looks identical to a pointer to a single
object, as far as the compiler is concerned &#8212; they don't contain
any information about the length. This is why the example function
above requires the length as a separate argument: within the
function, <CODE CLASS=C>sizeof first</CODE> would only give the size
of a pointer to an integer, not the length of the array.</P>

<H4><A NAME="arrayparam">Arrays as function parameters</A></H4>

<P>Note that a function parameter of array type isn't treated as an
array, but as a pointer (the array syntax is allowed, but only pointer
semantics are implemented). The following declaration is equivalent to
the one above:</P>

<PRE CLASS=C>
void fill_array_with_square_numbers(int first[], int length);
</PRE>

<P>Within the definition of this function, <CODE
CLASS=C>sizeof&nbsp;first</CODE> will still equal <CODE
CLASS=C>sizeof(int&nbsp;*)</CODE>, even if we place a length inside
the square brackets (such a value is ignored anyway).</P>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="const"><CODE CLASS=C>const</CODE> instead of <CODE
CLASS=JAVA>final</CODE></A></H3>

<P>Java uses the keyword <CODE CLASS=JAVA>final</CODE> to indicate
&#8216;variables&#8217; which can only be assigned to once (usually
where they are declared). C uses the keyword <CODE
CLASS=C>const</CODE> with an object declaration to indicate a constant
object that can (and must) be initialised, but cannot subsequently be
assigned to &#8212; it is not a variable, but it still has an address
and a size (so you can write <CODE CLASS=C>&amp;obj</CODE> or <CODE
CLASS=C>sizeof obj</CODE>).</P>

<PRE CLASS=C>
double sin(double); /* <SPAN CLASS=COMMENT>mathematical function sine</SPAN> */

const double pi = 3.14159;
double val;

val = sin(pi); /* <SPAN CLASS=COMMENT>legal expression</SPAN> */
pi = 3.0;      /* <SPAN CLASS=COMMENT>illegal; not a modifiable lvalue</SPAN> */
</PRE>


<H4><A NAME="ptrconst">Pointers to <CODE CLASS=C>const</CODE> objects</A></H4>

<P><CODE CLASS=C>const</CODE> is useful when declaring functions that
take pointers or arrays as arguments, but do not modify the
dereferenced contents:</P>

<PRE CLASS=C>
int sum(const int *ar, int len)
{
  int s = 0, i;

  for (i = 0; i < len; i++)
    s += ar[i];
  return s;
}

int array[] = { 1, 2, 4, 5 };

int total = sum(array, 4);
</PRE>

<P>The <CODE CLASS=C>const</CODE> assures us that the invocation will
not attempt to assign to <CODE CLASS=C>*array</CODE> (or <CODE
CLASS=C>array[1]</CODE>, <CODE CLASS=C>array[2]</CODE>, <SPAN
CLASS=ITALIC>etc</SPAN>).</P>


<H4><A NAME="constptr"><CODE CLASS=C>const</CODE> pointers</A></H4>

<P>Pointers themselves can be declared <CODE CLASS=C>const</CODE> just
like other objects. In these cases, the pointer can't be made to point
elsewhere, but what it points to can be modified (assuming that that
isn't further <CODE CLASS=C>const</CODE>-qualified). Careful
positioning of the keyword <CODE CLASS=C>const</CODE> is required to
distinguish constant pointers from pointers to constants:</P>

<PRE CLASS=C>
int array = { 1, 2, 4, 5 };
int *ip = array;               /* <SPAN CLASS=COMMENT>a pointer to an integer</SPAN> */
int *const ipc = array;        /* <SPAN CLASS=COMMENT>a constant pointer to an integer</SPAN> */
const int *const icpc = array; /* <SPAN CLASS=COMMENT>a constant pointer to a constant integer</SPAN> */

ipc[0] = ipc[1] + ipc[2];      /* <SPAN CLASS=COMMENT>okay</SPAN> */
ip += 2;                       /* <SPAN CLASS=COMMENT>okay</SPAN> */
ipc += 1;                      /* <SPAN CLASS=COMMENT>wrong; pointer is constant</SPAN> */
icpc[1] += 4;                  /* <SPAN CLASS=COMMENT>wrong; pointed-to object is constant</SPAN> */
</PRE>

<P>This example shows a modifiable array whose members are being
accessed through four pointers with slightly different types.</P>

</DIV>



<DIV CLASS=CONTAINER>

<H3><A NAME="inline">Inline functions</A></H3>

<P>C99 supports inline functions.  The programmer can indicate to the
compiler that a function's speed is critical by making it <CODE
CLASS=C>inline</CODE>:</P>

<PRE CLASS=C>
inline int square(int x)
{
  return x * x;
}
</PRE>

<P>If this definition is in scope, and you make a call to it, the
compiler may choose not to actually go through the overhead of calling
the function, but effectively place a copy of it inside the calling
function.</P>

<P>Inline function definitions can (and often should) appear in <A
HREF="#include">header files</A> instead of their <A
HREF="#protos">prototypes</A>.  A normal (&lsquo;external&rsquo;)
definition must still be provided &mdash; for example, some part of
your program may try to obtain a <A HREF="#funcptr">pointer</A> to the
function, and only a normal definition can provide that.</P>

<P>If the inline definition is in scope, an equivalent external
definition can be generated from it by simply redclaring the function
with <CODE CLASS=C>extern</CODE>:</P>

<PRE CLASS=C>
extern int square(int x);
</PRE>

<P>If the inline definition isn't in scope, you could provide a normal
definition which doesn't actually match the inline definition &mdash;
but this could lead to confusing behaviour.</P>

</DIV>



<DIV CLASS=CONTAINER>

<H3><A NAME="strings">Characters and strings</A></H3>

<P>A Java variable of type <CODE CLASS=JAVA>char</CODE> can hold any
Unicode character.  In C, the <CODE CLASS=C>char</CODE> type can
represent any character in a character set that depends on the type of
system or platform for which the program is compiled.  This is usually
a variation of US&nbsp;ASCII, but it doesn't have to be, so beware.
In particular, it could be a multibyte encoding, where a larger set of
characters are represented by several <CODE CLASS=C>char</CODE>
objects, e.g. UTF-8; a basic set of characters, however, are always
represented as single <CODE CLASS=C>char</CODE>s.</P>

<P>Java strings are objects of class <CODE
CLASS=JAVA>java.lang.String</CODE> or <CODE
CLASS=JAVA>java.lang.StringBuffer</CODE>, and represent sequences of
<CODE CLASS=JAVA>char</CODE>.</P>

<P>Strings in C are just arrays of, or pointers to, <CODE
CLASS=C>char</CODE>. Functions which handle strings typically assume
that the string is terminated with a null character <CODE
CLASS=C>'\0'</CODE>, rather than being passed length parameter. A
character array can be initialised like other arrays:</P>

<PRE CLASS=C>
char word[] = { 'H', 'e', 'l', 'l', 'o', '!', '\0' };
char another[] = "Hello!";
</PRE>

<P>Note that the second initialiser is a shorter form of the first,
including the terminating null character. Such a string literal can
also appear in an expression. It evaluates to a pointer to the first
character.</P>

<PRE CLASS=C>
const char *ptr;

ptr = "Hello!";
</PRE>

<P><CODE CLASS=C>ptr</CODE> now points to an anonymous, statically
allocated array of characters. Attempting to write to a string literal
like this has undefined behaviour, so the use of <CODE
CLASS=C>const</CODE> ensures that such attempts are detected while
compiling.</P>

<P>Utilities for handling character strings are declared in <CODE
CLASS=C>&lt;string.h&gt;</CODE>. For example, the function to copy a
string from one place to another is declared as:</P>

<PRE CLASS=C>
char *strcpy(char *to, const char *from);
</PRE>

<P>and may be used like this:</P>

<PRE CLASS=C>
#include &lt;string.h&gt;

char words[100];

strcpy(words, "Madam, I'm Adam.");
</PRE>

<P>Like many of the other <CODE CLASS=C>&lt;string.h&gt;</CODE>
functions, <CODE CLASS=C>strcpy</CODE> <EM>assumes</EM> that you have
already allocated sufficient space to store the string.</P>


<H4><A NAME="wchar_t">Wide characters</A></H4>

<P>To be done...</P>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="dynmem">Dynamic memory management</A></H3>

<P>Dynamic memory management is built into Java through its <CODE
CLASS=JAVA>new</CODE> keyword and its garbage collector. In C, it is
available through two functions in <CODE
CLASS=C>&lt;stdlib.h&gt;</CODE> which are declared as:</P>

<PRE CLASS=C>
void *malloc(size_t s); /* <SPAN CLASS=COMMENT>reserve memory for <CODE>s</CODE> <CODE>char</CODE>s</SPAN> */
void free(void *);      /* <SPAN CLASS=COMMENT>release memory reserved with <CODE>malloc()</CODE></SPAN> */
</PRE>

<P>(<CODE CLASS=C>size_t</CODE> is an alias for an unsigned integral type.)</P>

<P><CODE CLASS=C>malloc(s)</CODE> returns a pointer to the start of a
block of memory big enough for <CODE CLASS=C>s</CODE> <CODE
CLASS=C>char</CODE>s. It returns a generic pointer which can be
assigned to a pointer of any type. The memory is not initialised. All
such allocated memory must be released when it is no longer required
by passing a pointer to its start to <CODE CLASS=C>free()</CODE>. Only
pointer values returned by <CODE CLASS=C>malloc()</CODE> can be passed
to <CODE CLASS=C>free()</CODE>.</P>

<P>You can find out the amount of memory needed to store an object of
a particular type using <CODE
CLASS=C>sizeof(<VAR>type</VAR>)</CODE>. For an array, multiply this by
the required size of the array.</P>

<PRE CLASS=C>
long *lp;
long *lap;

lp = malloc(sizeof(long));
lap = malloc(sizeof(long) * 10);

/* <SPAN CLASS=COMMENT>now we can access <CODE>*lp</CODE> as a long integer,
   and <CODE>lap[0]</CODE>..<CODE>lap[9]</CODE> form an array</SPAN> */

free(lap);
free(lp);

/* <SPAN CLASS=COMMENT>now we can't</SPAN> */
</PRE>

<P><CODE CLASS=C>malloc()</CODE> returns a null pointer (<CODE
CLASS=C>0</CODE>) if it cannot allocate the requested amount of
memory.</P>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="noexcepts">Lack of exceptions</A></H3>

<P>Java supports exceptions to cover application-defined mistakes
as well as more serious system or memory-access errors (such as
accessing beyond the bounds of an array).</P>

<P>In C, application-defined error conditions are normally expressed
through careful definition of the meaning of values returned by
functions. More serious errors, such as an attempt to access memory
that hasn't been allocated in some way, may go unnoticed (because the
behaviour is undefined). Write-access to such memory may cause
corruption of critical hidden data, which only results in an error at
a later stage, so the original cause of the error may be difficult to
trace. <STRONG>Just because some activity is illegal in C, it doesn't
mean that you will necessarily be told about it, either by the
compiler or by the running program.</STRONG></P>

</DIV>

<DIV CLASS=CONTAINER>

<H3><A NAME="main"><CODE CLASS=C>main()</CODE> function</A></H3>

<P>In a Java application, execution begins in a static method
(<CODE CLASS=JAVA>void main(String[])</CODE>) of a specified class. In
C, execution also begins at a function called <CODE
CLASS=C>main</CODE>, but it has the following prototype:</P>

<PRE CLASS=C>
int main(int argc, char **argv);
</PRE>

<P>The parameters represent an array of character strings that form
the command that ran the program. <CODE CLASS=C>argv[0]</CODE> is
usually the name of the program, <CODE CLASS=C>argv[1]</CODE> is the
first argument, <CODE CLASS=C>argv[2]</CODE> is the second, ..., <CODE
CLASS=C>argv[argc&nbsp;-&nbsp;1]</CODE> is the last, and <CODE
CLASS=C>argv[argc]</CODE> is a null pointer. For example, the command</P>

<PRE>
myprog wibbly wobbly
</PRE>

<P>may cause <CODE CLASS=C>main</CODE> to be invoked as if by:</P>

<PRE CLASS=C>
char a1[] = "myprog";
char a2[] = "wibbly";
char a3[] = "wobbly";

char *argv[4] = { a1, a2, a3, NULL };

main(3, argv);
</PRE>

<P>The parameters are optional (you can replace them with a single
<CODE CLASS=C>void</CODE>), but <CODE CLASS=C>main</CODE> always
returns <CODE CLASS=C>int</CODE> in any portable program. Returning
<CODE CLASS=C>0</CODE> tells the environment that the program
completed successfully. Other values (implementation-defined) indicate
some sort of failure. <CODE CLASS=C>&lt;stdlib.h&gt;</CODE> defines
the macros <CODE CLASS=C>EXIT_SUCCESS</CODE> and <CODE
CLASS=C>EXIT_FAILURE</CODE> as symbolic return codes.</P>

</DIV>

<!--

<DIV CLASS=CONTAINER>

<H3><A NAME="stdio">Input/output</A></H3>

<P>Java provides classes to support I/O in the package <CODE
CLASS=JAVA>java.io</CODE>, which form various abstractions of
streams. The C header <CODE CLASS=C>&lt;stdio.h&gt;</CODE> specifies a
type to represent and open stream, <CODE CLASS=C>FILE</CODE>,
and declares some functions to read from or write to a stream using
raw or formatted data. The C objects <CODE CLASS=C>stdin</CODE>, <CODE
CLASS=C>stdout</CODE>, and <CODE CLASS=C>stderr</CODE> (which have
type <CODE CLASS=C>FILE&nbsp;*</CODE>) correspond to the Java objects
<CODE CLASS=JAVA>System.in</CODE>, <CODE CLASS=JAVA>System.out</CODE>
and <CODE CLASS=JAVA>System.err</CODE> in <CODE
CLASS=JAVA>java.lang</CODE>.</P>

<P>Rather than defining a function to output data for each type,
formatted output is achieved by passing a format string to a general
function which matches subsequent arguments to special directives
within the string:</P>

<PRE CLASS=C>
#include &lt;stdio.h&gt;

double height;
int age;

printf("Your height is %f; your age is %d.\n", height, age);
</PRE>

<P>The first argument to <CODE CLASS=C>printf</CODE> (which writes to
<CODE CLASS=C>stdout</CODE>) is the format string. It is printed
literally, except where a <CODE CLASS=C>%</CODE> directive is
found. In the example, the first directive causes the first argument
after the format string to be printed (the <CODE CLASS=C>f</CODE> says
that it is a <CODE CLASS=C>double</CODE> or a <CODE
CLASS=C>float</CODE>). The second directive corresponds to the second
argument after the format string, which is interpreted as an <CODE
CLASS=C>int</CODE>, <CODE CLASS=C>short</CODE> or <CODE CLASS=C>signed
char</CODE>, and is displayed as a decimal integer. <CODE
CLASS=C>printf</CODE> can take as many arguments as necessary, as
indicated by the format string.</P>

<P>Other directives are <CODE CLASS=C>%s</CODE> for a null-terminated
character string, <CODE CLASS=C>%u</CODE> for unsigned integral types,
<CODE CLASS=C>%x</CODE> for hexadecimal format, <CODE
CLASS=C>%o</CODE> for octal format, <SPAN
CLASS=ITALIC>etc</SPAN>. Additional characters following the <CODE
CLASS=C>%</CODE> specify additional formatting information (such as
justification, field width, precision) or type width (<CODE
CLASS=C>%ld</CODE> for a <CODE CLASS=C>long</CODE>).</P>

<P><CODE CLASS=C>printf</CODE> is one of a suite of output functions,
the others taking additional arguments to specify alternative
destination streams (<CODE CLASS=C>fprintf</CODE>), or character
arrays (<CODE CLASS=C>sprintf</CODE>). There is also a corresponding
suite of input functions based on the name <CODE
CLASS=C>scanf</CODE>.</P>

</DIV>

-->

<DIV CLASS=CONTAINER>

<H3><A NAME="stdlib">Standard library facilities</A></H3>

<P>Java comes with a rich and still-developing set of classes to
support I/O, networking, GUIs, <SPAN CLASS=ITALIC>etc</SPAN>, to
access a process's environment.</P>

<P>Similarly, the C language has a core of facilities to access its
environment. These functions, types and macros form C's Standard
Library. It is necessarily limited in order to support maximum
portability (it provides no GUI facilities, for example), but it is
largely fixed and stable. Access to other facilities (GUI, networking)
is through additional libraries that are usually specific to your
platform.</P>

<P>The headers of the C Standard Library are briefly summarised below:</P>

<DL>

<DT><CODE CLASS=C>&lt;stddef.h&gt;</CODE></DT>

<DD><P>Some essential macros and additional type declarations</P></DD>

<DT><CODE CLASS=C>&lt;stdlib.h&gt;</CODE></DT>

<DD><P>Access to environment; dynamic memory allocation; miscellaneous
utilities</P></DD>

<DT><CODE CLASS=C>&lt;stdio.h&gt;</CODE></DT>

<DD><P>Streamed input and output of characters</P></DD>

<DT><CODE CLASS=C>&lt;string.h&gt;</CODE></DT>

<DD><P>String handling</P></DD>

<DT><CODE CLASS=C>&lt;ctype.h&gt;</CODE></DT>

<DD><P>Classification of characters (upper/lower case,
alphabetic/numeric <SPAN CLASS=ITALIC>etc</SPAN>)</P></DD>

<DT><CODE CLASS=C>&lt;limits.h&gt;</CODE></DT>

<DD><P>Implementation-defined limits for integral types</P></DD>

<DT><CODE CLASS=C>&lt;float.h&gt;</CODE></DT>

<DD><P>Implementation-defined limits for floating-point types</P></DD>

<DT><CODE CLASS=C>&lt;math.h&gt;</CODE></DT>

<DD><P>Mathematical functions</P></DD>

<DT><CODE CLASS=C>&lt;assert.h&gt;</CODE></DT>

<DD><P>Diagnostic utilities</P></DD>

<DT><CODE CLASS=C>&lt;errno.h&gt;</CODE></DT>

<DD><P>Error identification</P></DD>

<DT><CODE CLASS=C>&lt;locale.h&gt;</CODE></DT>

<DD><P>Regional/national variations in character sets, time
formats, <SPAN CLASS=ITALIC>etc</SPAN></P></DD>

<DT><CODE CLASS=C>&lt;stdarg.h&gt;</CODE></DT>

<DD><P>Support for functions with variable numbers of arguments</P></DD>

<DT><CODE CLASS=C>&lt;time.h&gt;</CODE></DT>

<DD><P>Representations of time, and clock access</P></DD>

<DT><CODE CLASS=C>&lt;signal.h&gt;</CODE></DT>

<DD><P>Handling of exceptional run-time events</P></DD>

<DT><CODE CLASS=C>&lt;setjmp.h&gt;</CODE></DT>

<DD><P>Restoration of execution to a previous state</P></DD>

</DL>

<P>C95 additionally provides the following headers:</P>

<DL>

<DT><CODE CLASS=C>&lt;iso646.h&gt;</CODE></DT>

<DD><P>Alphabetic names for operators</P></DD>

<DT><CODE CLASS=C>&lt;wchar.h&gt;</CODE></DT>

<DD><P>Manipulation of wide-character streams and strings</P></DD>

<DT><CODE CLASS=C>&lt;wctype.h&gt;</CODE></DT>

<DD><P>Classification of wide characters (upper/lower case,
alphabetic/numeric <SPAN CLASS=ITALIC>etc</SPAN>)</P></DD>

</DL>

<P>C99 additionally provides the following headers:</P>

<DL>

<DT><CODE CLASS=C>&lt;stdbool.h&gt;</CODE></DT>

<DD><P>The boolean type and constants</P></DD>

<DT><CODE CLASS=C>&lt;complex.h&gt;</CODE></DT>

<DD><P>The complex types and constants</P></DD>

<DT><CODE CLASS=C>&lt;inttypes.h&gt;</CODE></DT>
<DT><CODE CLASS=C>&lt;stdint.h&gt;</CODE></DT>

<DD><P>Integer types of specific or minimum widths</P></DD>

<DT><CODE CLASS=C>&lt;fenv.h&gt;</CODE></DT>

<DD><P>Access to the floating-point environment</P></DD>

<DT><CODE CLASS=C>&lt;tgmath.h&gt;</CODE></DT>

<DD><P>Type-generic mathematics functions</P></DD>

</DL>

</DIV>

<!--

<HR>

<H2>Footnotes</H2>

<UL TYPE=1>

<LI VALUE=1><P><A NAME="foot1"></A>Technically, the ANSI/ISO standard
says that an lvalue is an expression that refers to a location in
memory, whether you can write to that memory or not. It then defines a
<DFN>modifiable lvalue</DFN> as one which can appear on the left-hand
side of an assignment, and says that array names and <CODE
CLASS=C>const</CODE>-qualified objects aren't modifiable lvalues.</P>

<LI VALUE=2><P><A NAME="foot2">The term &quot;specify&quot; has a
particular meaning within the ANSI/ISO standard, but it has been used
loosely here in order to avoid confusion with &quot;declare&quot; and
&quot;define&quot; which have been allocated to objects and functions
but may also apply to types.</A></P>

</UL>

-->

</BODY>

</HTML>
